// 本地存储工具
import { STORAGE_KEYS } from './constants.js'

// 存储数据
export const setStorage = (key, data) => {
  try {
    wx.setStorageSync(key, data)
    return true
  } catch (error) {
    console.error('存储数据失败:', error)
    return false
  }
}

// 获取数据
export const getStorage = (key, defaultValue = null) => {
  try {
    const data = wx.getStorageSync(key)
    return data !== '' ? data : defaultValue
  } catch (error) {
    console.error('获取数据失败:', error)
    return defaultValue
  }
}

// 删除数据
export const removeStorage = (key) => {
  try {
    wx.removeStorageSync(key)
    return true
  } catch (error) {
    console.error('删除数据失败:', error)
    return false
  }
}

// 清空所有数据
export const clearStorage = () => {
  try {
    wx.clearStorageSync()
    return true
  } catch (error) {
    console.error('清空数据失败:', error)
    return false
  }
}

// 获取存储信息
export const getStorageInfo = () => {
  try {
    return wx.getStorageInfoSync()
  } catch (error) {
    console.error('获取存储信息失败:', error)
    return null
  }
}

// 异步存储数据
export const setStorageAsync = (key, data) => {
  return new Promise((resolve, reject) => {
    wx.setStorage({
      key,
      data,
      success: () => resolve(true),
      fail: (error) => {
        console.error('异步存储数据失败:', error)
        reject(error)
      }
    })
  })
}

// 异步获取数据
export const getStorageAsync = (key, defaultValue = null) => {
  return new Promise((resolve, reject) => {
    wx.getStorage({
      key,
      success: (res) => resolve(res.data),
      fail: (error) => {
        if (error.errMsg.includes('data not found')) {
          resolve(defaultValue)
        } else {
          console.error('异步获取数据失败:', error)
          reject(error)
        }
      }
    })
  })
}

// 异步删除数据
export const removeStorageAsync = (key) => {
  return new Promise((resolve, reject) => {
    wx.removeStorage({
      key,
      success: () => resolve(true),
      fail: (error) => {
        console.error('异步删除数据失败:', error)
        reject(error)
      }
    })
  })
}

// 用户相关存储
export const setUserInfo = (userInfo) => {
  return setStorage(STORAGE_KEYS.USER_INFO, userInfo)
}

export const getUserInfo = () => {
  return getStorage(STORAGE_KEYS.USER_INFO)
}

export const removeUserInfo = () => {
  return removeStorage(STORAGE_KEYS.USER_INFO)
}

// Token相关存储
export const setToken = (token) => {
  return setStorage(STORAGE_KEYS.TOKEN, token)
}

export const getToken = () => {
  return getStorage(STORAGE_KEYS.TOKEN)
}

export const removeToken = () => {
  return removeStorage(STORAGE_KEYS.TOKEN)
}

// 购物车相关存储
export const setCartData = (cartData) => {
  return setStorage(STORAGE_KEYS.CART_DATA, cartData)
}

export const getCartData = () => {
  return getStorage(STORAGE_KEYS.CART_DATA, [])
}

export const removeCartData = () => {
  return removeStorage(STORAGE_KEYS.CART_DATA)
}

// 地址相关存储
export const setAddressData = (addressData) => {
  return setStorage(STORAGE_KEYS.ADDRESS_DATA, addressData)
}

export const getAddressData = () => {
  return getStorage(STORAGE_KEYS.ADDRESS_DATA, [])
}

export const removeAddressData = () => {
  return removeStorage(STORAGE_KEYS.ADDRESS_DATA)
}

// 清除用户相关数据
export const clearUserData = () => {
  removeUserInfo()
  removeToken()
  removeCartData()
  removeAddressData()
}

export default {
  setStorage,
  getStorage,
  removeStorage,
  clearStorage,
  getStorageInfo,
  setStorageAsync,
  getStorageAsync,
  removeStorageAsync,
  setUserInfo,
  getUserInfo,
  removeUserInfo,
  setToken,
  getToken,
  removeToken,
  setCartData,
  getCartData,
  removeCartData,
  setAddressData,
  getAddressData,
  removeAddressData,
  clearUserData
}
